import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 当前索引
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: IndexedStack(
        index: currentIndex,
        children: const [],
      )),
      bottomNavigationBar: BottomNavigationBar(
        items: barItemList(),
        // tabbar类型
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Colors.deepOrange,
        unselectedItemColor: Colors.black,
        currentIndex: currentIndex,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> items = [];
    items.add(BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/home.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/home-active.png',
          width: 40,
          height: 40,
        ),
        label: '首页'));
    items.add(BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/cate.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/cate-active.png',
          width: 40,
          height: 40,
        ),
        label: '分类'));
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/cart.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/cart-active.png',
            width: 40,
            height: 40,
          ),
          label: '购物车'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/user.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/user-active.png',
            width: 40,
            height: 40,
          ),
          label: '我的'),
    );
    return items;
  }
}
